package wktest;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.boot.jackson.JsonObjectSerializer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisClient {

	private Jedis jedis;//非切片额客户端连接  
    private JedisPool jedisPool;//非切片连接池  
    private ShardedJedis shardedJedis;//切片额客户端连接  
    private ShardedJedisPool shardedJedisPool;//切片连接池  
      
    public RedisClient()   
    {   
        initialPool();   
        initialShardedPool();   
        shardedJedis = shardedJedisPool.getResource();   
        jedis = jedisPool.getResource();   
          
          
    }   
   
    /** 
     * 初始化非切片池 
     */  
    private void initialPool()   
    {   
        // 池基本配置   
        JedisPoolConfig config = new JedisPoolConfig();   
        config.setMaxIdle(20);   
        config.setMaxIdle(5);   
        config.setMaxWaitMillis(1000l);   
        config.setTestOnBorrow(false);   
          
        jedisPool = new JedisPool(config,"127.0.0.1",6379);  
    }  
      
    /**  
     * 初始化切片池  
     */   
    private void initialShardedPool()   
    {   
        // 池基本配置   
        JedisPoolConfig config = new JedisPoolConfig();   
        config.setMaxIdle(20);   
        config.setMaxIdle(5);   
        config.setMaxWaitMillis(1000l);   
        config.setTestOnBorrow(false);   
        // slave链接   
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();   
        shards.add(new JedisShardInfo("127.0.0.1", 6379, "master"));   
  
        // 构造池   
        shardedJedisPool = new ShardedJedisPool(config, shards);   
    }   
  
    public void show() {       
        KeyOperate();   
        StringOperate();   
        ListOperate();   
        SetOperate();  
        SortedSetOperate();  
        HashOperate();   
        jedisPool.returnResource(jedis);  
        shardedJedisPool.returnResource(shardedJedis);  
    }   
  
      private void KeyOperate() {  
      }  
  
      private void StringOperate() {  
      }  
  
      private void ListOperate() {  
    	  List<UserObject> list = new ArrayList<UserObject>();
    	  UserObject user;
    	  long start = System.currentTimeMillis();
    	  for(int i = 0; i<100000; i ++){
    		  user = new UserObject();
    		  user.setUserId(UUID.randomUUID().toString());
    		  user.setAge(String.valueOf(i));
    		  user.setEmail("weikuan@huawei.com");
    		  user.setTel("13430418853");
    		  list.add(user);
    	  }
    	  String jsonString = JSON.toJSONString(list);
    	  shardedJedis.set("userList", jsonString);
    	 
    	 // shardedJedis.set("userList".getBytes(), SerializeUtil.serialize(list));  
    	 // byte[] userlist = shardedJedis.get(("userList").getBytes());
    	  
    	  //@SuppressWarnings("unchecked")
    	 // List<UserObject> ulist = (List<UserObject>) SerializeUtil.unserialize(userlist);
    	 String str =  shardedJedis.get("userList");
    	 List<UserObject> ulist = JSON.parseObject(str, List.class);
    	 long end = System.currentTimeMillis();
    	 
    	  if(ulist != null){
    		  System.out.println("ulist" + ulist.size());
    		  System.out.println("\r<br>执行耗时 : "+(end-start)/1000f+" 秒 ");
    	  }
      }  
  
      private void SetOperate() {  
      }  
  
      private void SortedSetOperate() {  
      }  
      
      private void HashOperate() {  
      }  
}
